package com.ansion.basedb.storage;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.security.crypto.EncryptedSharedPreferences;

import com.ansion.basedb.storage.api.EncSpStorage;
import com.ansion.utils.ResourceUtils;
import com.ansion.utils.hash.Base64Helper;

/**
 * <pre>
 *     author : jake
 *     e-mail : hongjie.wang@ctechm.com
 *     time   : 2022/3/2 18:11
 *     function   : 区分用户的加密存储
 * </pre>
 */
public class SecureStorePreference implements EncSpStorage {
    public static final String SECURE_STORE_FILE_NAME = "esp_";
    public static final String DEFAULT_MASTER_KEY_ALIAS = "_androidx_security_master_key_";
    private String name;
    private SharedPreferences esp;
    private SharedPreferences.Editor editor;

    public SecureStorePreference(String name) throws Throwable {
        this.name = name;
        esp = createEsp(ResourceUtils.getContext());
        editor = esp.edit();
    }

    @Override
    public boolean remove(@NonNull String key) {
        return editor.remove(key).commit();
    }

    @Override
    public boolean contains(@NonNull String key) {
        return esp.contains(key);
    }

    @Override
    public boolean putInt(@NonNull String key, int value) {
        return editor.putInt(key, value).commit();
    }

    @Override
    public int getInt(@NonNull String key, int defValue) {
        return esp.getInt(key, defValue);
    }

    @Override
    public boolean putLong(@NonNull String key, long value) {
        return editor.putLong(key, value).commit();
    }

    @Override
    public long getLong(@NonNull String key, long defValue) {
        return esp.getLong(key, defValue);
    }

    @Override
    public boolean putFloat(@NonNull String key, float value) {
        return editor.putFloat(key, value).commit();
    }

    @Override
    public float getFloat(@NonNull String key, float defValue) {
        return esp.getFloat(key, defValue);
    }

    public boolean putString(String key, String value) {
        return esp.edit().putString(key, value).commit();
    }

    public String getString(String key, String defaultValue) {
        return esp.getString(key, defaultValue);
    }

    @Override
    public boolean putBoolean(@NonNull String key, boolean value) {
        return editor.putBoolean(key, value).commit();
    }

    @Override
    public boolean getBoolean(@NonNull String key, boolean defValue) {
        return esp.getBoolean(key, defValue);
    }

    @Override
    public boolean putByteArray(@NonNull String key, byte[] value) {
        return editor.putString(key, Base64Helper.encode(value)).commit();
    }

    @Override
    public byte[] getByteArray(@NonNull String key, byte[] defValue) {
        String value = esp.getString(key, "");
        if (TextUtils.isEmpty(value)) {
            return defValue;
        }
        return Base64Helper.decode(value);
    }

    private SharedPreferences createEsp(@NonNull Context context) throws Throwable {
        return EncryptedSharedPreferences.create(getName(name), DEFAULT_MASTER_KEY_ALIAS,
                context,
                EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
        );
    }

    public static String getName(String name) {
        return SECURE_STORE_FILE_NAME + name;
    }


    @Override
    public void clearAll() {
        editor.clear().commit();
    }
}
